package cn.stylefeng.guns.modular.tool.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicCustomer;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.tool.entity.ToolGoodsInstall;
import cn.stylefeng.guns.modular.tool.mapper.ToolGoodsInstallMapper;
import cn.stylefeng.guns.modular.tool.model.params.ToolExpressScanParam;
import cn.stylefeng.guns.modular.tool.model.params.ToolGoodsInstallParam;
import cn.stylefeng.guns.modular.tool.model.result.ToolGoodsInstallResult;
import  cn.stylefeng.guns.modular.tool.service.ToolGoodsInstallService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

/**
 * 产品重量设置 服务实现类
 */
@Service
public class ToolGoodsInstallServiceImpl extends ServiceImpl<ToolGoodsInstallMapper, ToolGoodsInstall> implements ToolGoodsInstallService {

    @Override
    public void add(ToolGoodsInstallParam param){
        ToolGoodsInstall entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(ToolGoodsInstallParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(ToolGoodsInstallParam param){
        ToolGoodsInstall oldEntity = getOldEntity(param);
        ToolGoodsInstall newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ToolGoodsInstallResult findBySpec(ToolGoodsInstallParam param,Long operateId){
        return this.baseMapper.findBySpec(param,operateId);
    }

    @Override
    public List<ToolGoodsInstallResult> findListBySpec(ToolGoodsInstallParam param,Long operateId){
        return this.baseMapper.findListBySpec(param, operateId);
    }

    @Override
    public LayuiPageInfo findPageBySpec(ToolGoodsInstallParam param,String condition,Long operateId){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition, operateId);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ToolGoodsInstallParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ToolGoodsInstall getOldEntity(ToolGoodsInstallParam param) {
        return this.getById(getKey(param));
    }

    private ToolGoodsInstall getEntity(ToolGoodsInstallParam param) {
        ToolGoodsInstall entity = new ToolGoodsInstall();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }
    
    @Resource
    private UserService userService;
    
    @Resource
    private BasicCustomerService basicCustomerService;

	@Override
	@Transactional(rollbackFor = Exception.class,timeout = 600)
	public ResponseData getExceldata(MultipartFile file,Long customerId, ResponseData data) throws IOException{
		User user = userService.getcurrentUser();
        BasicCustomer basicCustomer = basicCustomerService.getById(customerId);
		String fileName = file.getOriginalFilename();
		List<ToolGoodsInstallParam> list = new ArrayList<ToolGoodsInstallParam>();
		
		if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
			data.setMessage("上传文件格式不正确");
			data.setSuccess(false);
        }

		boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        Sheet sheet = wb.getSheetAt(0);
        
        for (int r = 1; r <= sheet.getLastRowNum()+1; r++) {
        	Row row = sheet.getRow(r);
            if (row == null){
                continue;
            }
            ToolGoodsInstallParam param = new ToolGoodsInstallParam();

            String  orderNumber = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(0));
            if (ToolUtil.isNotEmpty(orderNumber)) {
    			param.setOrderNumber(orderNumber);
			} 
            
            String  goodsNumber = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(1));
            if (ToolUtil.isNotEmpty(goodsNumber)) {
    			param.setGoodsNumber(goodsNumber);
    			ToolGoodsInstallParam paramcheck = new ToolGoodsInstallParam();
    			paramcheck.setCustomerId(customerId);
    			paramcheck.setGoodsNumber(goodsNumber);
    			ToolGoodsInstallResult result = this.findBySpec(paramcheck, null);
    			if(result != null) {
    				continue;
    			}
			} else {
				continue;
			}
            
            String  weight = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(2));
            if (ToolUtil.isNotEmpty(weight)) {
            	BigDecimal weights = new BigDecimal(weight).setScale(3, BigDecimal.ROUND_HALF_DOWN );
            	param.setWeight(weights+"");
			} else {
				continue;
			}
            
            String  v1 = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(3));
            if (ToolUtil.isNotEmpty(v1)) {
    			param.setV1(v1);
			} 
            
            String  remarks = cn.stylefeng.guns.core.util.DateUtil.getCellValueByCell(row.getCell(4));
            if (ToolUtil.isNotEmpty(remarks)) {
    			param.setRemarks(remarks);
			} 
            param.setCustomerId(customerId);
            param.setCustomerName(basicCustomer.getCustomerName());
            param.setDeptId(user.getDeptId());
            list.add(param);
        }
        this.saveBatchList(list);
		return ResponseData.success();
	}

	@Override
	public boolean saveBatchList(List<ToolGoodsInstallParam> savelist) {
		return this.baseMapper.saveBatchList(savelist);
	}

}
